package com.platform.common.core.web.domain;

import com.platform.common.core.constant.HttpStatus;
import com.platform.common.core.utils.StringUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Objects;

/**
 * @Description: 统一接口返回类
 * @Author: wcs
 * @Date: 2024-12-13 15:05
 **/
@Data
@ApiModel("统一请求返回")
public class MbResult<T> {

    @ApiModelProperty("状态码")
    private Integer code;

    @ApiModelProperty("错误信息/成功信息")
    private String msg;

    @ApiModelProperty("响应数据")
    private T data;

    public MbResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public MbResult(int code, String msg, T data) {
        this(code, msg);
        if (StringUtils.isNotNull(data)) {
            this.data = data;
        }
    }

    public static <T> MbResult<T> success() {
        return MbResult.success("操作成功");
    }

    public static <T> MbResult<T> success(T data) {
        return MbResult.success("操作成功", data);
    }

    public static <T> MbResult<T> success(String msg) {
        return MbResult.success(msg, null);
    }

    public static <T> MbResult<T> success(String msg, T data) {
        return new MbResult<>(HttpStatus.SUCCESS, msg, data);
    }

    public static <T> MbResult<T> warn(String msg) {
        return MbResult.warn(msg, null);
    }

    public static <T> MbResult<T> warn(String msg, T data) {
        return new MbResult<>(HttpStatus.WARN, msg, data);
    }

    public static MbResult<?> error() {
        return MbResult.error("操作失败");
    }

    public static <T> MbResult<T> error(String msg) {
        return MbResult.error(msg, null);
    }

    public static <T> MbResult<T> error(String msg, T data) {
        return new MbResult<>(HttpStatus.ERROR, msg, data);
    }

    public static <T> MbResult<T> error(int code, String msg) {
        return new MbResult<>(code, msg, null);
    }

    public boolean isSuccess() {
        return Objects.equals(HttpStatus.SUCCESS, this.code);
    }

    public boolean isError() {
        return !isSuccess();
    }
}
